# ---- PVCS revision log handling ----------------------------

import os, string
import vcs
from logParser import *

import PVCSConstants
import re

# initialize Call Interface
from PVCSConstants import usedCallingInterface
from PVCSConstants import callWrapper

MONTHS = {
		'Jan' : '01',
		'Feb' : '02',
		'Mar' : '03',
		'Apr' : '04',
		'May' : '05',
		'Mai' : '05',
		'Jun' : '06',
		'Jul' : '07',
		'Aug' : '08',
		'Sep' : '09',
		'Oct' : '10',
		'Okt' : '10',
		'Nov' : '11',
		'Dec' : '12',
		'Dez' : '12'
		}

class Vlog(Log):
	REV_SEP     = '-'*35
	REV_SEPn    = REV_SEP + NL
	END_OF_LOG  = '='*35
	END_OF_LOGn = END_OF_LOG + NL
	SKIP_TAGS   = { 'attributes': None }
	CMD		= 'vlog -q -i %s'
	allBranchList = []
	all_revision_numbers = []

	def open(self, cmd, file):
		if callWrapper == None:
			self.log = vcs.popen(cmd, file)
		else:
			self.log = callWrapper.doCall("vlog_popen" , cmd, file)
		self.closed=FALSE

	def parse_history(self):
		maxi = len(self.history)-1
		if maxi < 5:
			return
		i = 0

		# change 2-26-99
		self.all_revision_numbers = []
		if PVCSConstants.VLOGTEST:
			print "(PVCS Log Parser)\tall_revision_numbers' @B%s@B'" %self.all_revision_numbers
		while i<=maxi:
			if self.history[i] != self.REV_SEP:
				break
			i=i+1
			fields = string.split(self.history[i])	# Rev 1.1
			rev = Revision(fields[1])
			self.all_revision_numbers.append("%s" % fields[1])
			i=i+1
			fields = string.split(self.history[i])	# Locked by:	xyz
			if len(fields) and fields[0]=='Locked':
				rev.locked_by = string.join(fields[2:])
				i=i+1
				fields = string.split(self.history[i]) 
			# Checked in: DD MMM YYYY HH:MM:SS
			# or
			# Checked in: MMM DD YYYY HH:MM:SS
			[d1, d2, d3, time] = fields[2:]
			dl = [d1,d2,d3]
			dl.sort(lambda a,b: cmp(len(a),len(b)))
			[day, month, year] = dl
			rev.date = '%s/%s/%s %s' % (year, MONTHS[month[:3]], day, time)
			i=i+2			# skip Last modified:
			fields = string.split(self.history[i], 'lines')
			# Author id: xyz  lines d/a/m: 0/0/0
			rev.author = string.strip(string.split(fields[0],':')[1])
			change = string.strip(string.split(fields[1],':')[1])
			change = string.split(change,'/')
			rev.lines = '+%s -%s' % (change[1], change[0])
			i=i+1
			fields = string.split(self.history[i])
			if PVCSConstants.VLOGTEST:
				print "(PVCS Log Parser)\tfields = @b",
				print fields,
				print "@x"
			if len(fields) > 0 and fields[0]=='Branches:':  # Branches:
				rev.branches = ''
				for aBranch in fields[1:]:
					rev.branches = rev.branches + ' ' + '%s' % aBranch
					self.allBranchList.append('%s' % aBranch)
				i=i+1
			msglist = []
			while i<=maxi and self.history[i] != self.REV_SEP:
				msglist.append(self.history[i])
				i=i+1
			rev.log_message = string.join(msglist, NL)
			self.revisions.append(rev)

		# change 2-26-99
		if PVCSConstants.VLOGTEST:
			print "(PVCS Log Parser)\tall_revision_numbers' @B%s@B'" %self.all_revision_numbers

	def head_rev(self):
		return self.last_trunk_rev
	def lockers(self):
		l = string.split(self.locks, ':')
		l = map(string.strip, l)
		return string.join(l, ':')
	def revision_numbers(self):
		return self.all_revision_numbers
	def symbolstr(self):
		return string.join(self.symbollist(), NL)
	def searchNewestRevisionFor(self, revision):
		all_revisions = self.revision_list()
		max_matched_revision = ""
		regexString = PVCSConstants.makeRegEx(revision)
		if PVCSConstants.VLOGTEST:
			print "(PVCS Log Parser)\tregexPattern = '%s'" % regexString
		for aRevision in all_revisions:
			splittedRevisions = string.split("'%s'" % aRevision)
			if len(splittedRevisions) >= 2:
				aRevisionNumber = splittedRevisions[1]
				if re.match(regexString, aRevisionNumber, 0) != None:
					if PVCSConstants.VLOGTEST:
						print "found revision: '%s'" % aRevisionNumber
					if aRevisionNumber > max_matched_revision:
						max_matched_revision = aRevisionNumber
		if PVCSConstants.VLOGTEST:
			print "(PVCS Log Parser)\tmax_matched_revision = '%s'" % max_matched_revision
		return max_matched_revision

	def symbollist(self):
		symbols = []
		if self.version_labels:
			if PVCSConstants.VLOGTEST:
				print "(PVCS Log Parser)\tversion_labels = @r",
				print self.version_labels,
				print "@x"
			for line in string.split(self.version_labels, NL):
				i = string.rfind(line, '"')
				sym = line[1:i]
				# translate Label
				TRANS2UNDERSCORE = string.maketrans(' :*"', '____')
				sym = string.translate(sym, TRANS2UNDERSCORE)
				i = string.rfind(line, ' ')
				rev = line[i+1:]
				# is floating label ?
				if string.find(rev, '*') != -1:
					if PVCSConstants.VLOGTEST:
						print "(PVCS Log Parser)\trev = '%s'" % rev
					rev = self.searchNewestRevisionFor(rev)
				symbols.append('%s: %s' % (sym, rev))
		return symbols
	def _work_file(self):
		return self.workfile
